package com.wechat.page.chat.message.msg;

import java.util.List;

import com.swak.entity.YesNo;
import com.swak.fx.support.ability.DragAbleAbility;
import com.swak.fx.support.ui.UIImage;
import com.swak.fx.support.ui.UILifecycle;
import com.wechat.dto.MessageDTO;
import com.wechat.face.Face;
import com.wechat.face.Faces;
import com.wechat.msg.TxtMsg;

import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.scene.Node;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;
import javafx.scene.text.TextFlow;

/**
 * 文本消息
 * 
 * @author 618lf
 */
public class TxtMsgComp extends AnchorPane implements MsgComp, UILifecycle {

	private final String STYLE_CLASS = "message__text";
	private final String ARROWS_STYLE_CLASS = "message__text-arrows";
	private final String INNER_STYLE_CLASS = "message__text-inner";
	private final String TEXT_STYLE_CLASS = "message__text-text";

	private final MessageDTO message;

	private Pane arrows;
	private TextFlow textPane;

	/**
	 * 是否是我发送的
	 */
	private boolean sender = false;

	public TxtMsgComp(MessageDTO message) {
		this.sender = message.getIsSender() == YesNo.Yes.getType() ? true : false;
		this.message = message;
		this.arrows = new Pane();
		this.textPane = new TextFlow();
		this.init();
	}

	@Override
	public void initializeParts() {
		this.getStyleClass().add(STYLE_CLASS);
		this.arrows.getStyleClass().add(ARROWS_STYLE_CLASS);
		this.textPane.getStyleClass().add(INNER_STYLE_CLASS);
		this.setMaxWidth(Max_Width);
		this.textPane.setMaxWidth(Max_Width);
		this.textPane.setLineSpacing(4);

		this.arrows.setPrefWidth(10);
		this.arrows.setPrefHeight(10);
		this.arrows.setRotate(45);
		this.textPane.setTextAlignment(TextAlignment.JUSTIFY);

		this.getChildren().addAll(arrows, textPane);

		if (!this.sender) {
			AnchorPane.setLeftAnchor(arrows, 0.0);
			AnchorPane.setTopAnchor(arrows, 12.0);
			AnchorPane.setTopAnchor(textPane, 0.0);
			AnchorPane.setLeftAnchor(textPane, 4.0);
			AnchorPane.setRightAnchor(textPane, 0.0);
		} else {
			AnchorPane.setRightAnchor(arrows, 0.0);
			AnchorPane.setTopAnchor(arrows, 12.0);
			AnchorPane.setTopAnchor(textPane, 0.0);
			AnchorPane.setRightAnchor(textPane, 4.0);
			AnchorPane.setLeftAnchor(textPane, 0.0);
		}
	}

	@Override
	public void setupValueChangedListeners() {
		this.prefHeightProperty().bind(this.textPane.heightProperty());
		this.textProperty.addListener((ob, o, n) -> {
			List<Object> texts = Faces.parseString(n);
			for (Object text : texts) {
				if (text instanceof String) {
					this.textPane.getChildren().add(this.buildText((String) text));
				} else if (text instanceof Face) {
					this.textPane.getChildren().add(this.buildImage((Face) text));
				}
			}
		});
	}

	private Text buildText(String text) {
		Text textNode = new Text();
		textNode.setText(text);
		textNode.getStyleClass().add(TEXT_STYLE_CLASS);
		textNode.setTextAlignment(TextAlignment.JUSTIFY);
		return textNode;
	}

	private Node buildImage(Face face) {
		UIImage image = new UIImage();
		image.setPrefSize(face.getWidth(), face.getHeight());
		image.image(face.getRealPath());
		image.getStyleClass().add(TEXT_STYLE_CLASS);
		return image;
	}

	@Override
	public void initializeFinish() {

		// 序列化消息
		TxtMsg msg = TxtMsg.fromXml(message.getMsgContent(), TxtMsg.class);

		// 设置消息文本
		this.textProperty.set(msg == null ? "已清理" : msg.getContent());

		// 添加可移动的能力
		new DragAbleAbility(this.textPane, () -> message);
	}

	private final StringProperty textProperty = new SimpleStringProperty();
}